package com.shenmazong.servereduuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shenmazong.servereduuser.entity.TbCollage;
import com.shenmazong.servereduuser.mapper.TbCollageMapper;
import com.shenmazong.servereduuser.service.TbCollageService;
import com.shenmazong.utils.Idutils;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.CollageVo;
import com.shenmazong.vo.ListParamVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 *
 */
@Service
public class TbCollageServiceImpl implements TbCollageService {

    public final static Integer COLLAGE_MIN_NO = 1000;

    @Autowired
    TbCollageMapper tbCollageMapper;

    private Integer getCollageNo() {
        TbCollage one = tbCollageMapper.selectOne(
                new QueryWrapper<TbCollage>().lambda().orderByDesc(TbCollage::getCollageNo).last("limit 1")
        );
        if(one == null) {
            return COLLAGE_MIN_NO;
        }

        return one.getCollageNo() + 1;
    }

    @Override
    public ResultResponse add(CollageVo collage) {

        TbCollage one = tbCollageMapper.selectOne(
                new QueryWrapper<TbCollage>().lambda().eq(TbCollage::getCollageName, collage.getCollageName()).last("limit 1")
        );
        if(one != null) {
            return ResultResponse.FAILED(400, "学院已经存在");
        }

        //
        TbCollage tbCollage = new TbCollage();
        BeanUtils.copyProperties(collage, tbCollage);

        tbCollage.setCollageId(Idutils.getId());
        tbCollage.setCollageNo(getCollageNo());

        tbCollageMapper.insert(tbCollage);

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse del(CollageVo collage) {

        //--1
        if(collage.getCollageId() == null || collage.getCollageId() == 0) {
            return ResultResponse.FAILED(400, "学院ID不正确");
        }

        //--2
        tbCollageMapper.deleteById(collage.getCollageId());

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse get(CollageVo collage) {
        //--1
        if(collage.getCollageId() == null || collage.getCollageId() == 0) {
            return ResultResponse.FAILED(400, "学院ID不正确");
        }

        //--2
        TbCollage tbCollage = tbCollageMapper.selectById(collage.getCollageId());
        if(tbCollage == null) {
            return ResultResponse.FAILED(404, "信息不存在");
        }

        //--3
        BeanUtils.copyProperties(tbCollage, collage);

        return ResultResponse.SUCCESS(collage);
    }

    @Override
    public ResultResponse set(CollageVo collage) {
        //--1
        if(collage.getCollageId() == null || collage.getCollageId() == 0) {
            return ResultResponse.FAILED(400, "学院ID不正确");
        }

        //--2
        TbCollage tbCollage = tbCollageMapper.selectById(collage.getCollageId());
        if(tbCollage == null) {
            return ResultResponse.FAILED(404, "信息不存在");
        }

        //--3
        BeanUtils.copyProperties(collage, tbCollage);
        tbCollageMapper.updateById(tbCollage);

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse page(ListParamVo param) {

        Page pg = new Page(param.getCurrent(), param.getSize());

        Page page = tbCollageMapper.selectPage(pg, null);

        return ResultResponse.SUCCESS(page);
    }
}




